package com.wb.oldwomen;

import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.wb.fw.GateActivity;
import com.wb.fw.Global;
import com.wb.fw.IConstants;
import com.wb.fw.SoundManager;
import com.wb.fw.Util;
import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.actions.JumpTo;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.utils.TargetSelector;

public class UILevelSelect extends ColorLayer implements Ioldwomam {
	public static UILevelSelect make(GameScene parent) {
		return new UILevelSelect(parent);
	}
	GameScene mparent;
	int mCurLvl;
	Sprite mSelect;
	Label mscore;
	protected UILevelSelect(GameScene parent) {
		mparent = parent;
		setKeyEnabled(true);
		setTouchEnabled(true);
		
		loadLockedInfo();
		
		Sprite bg = Sprite.make(R.drawable.level_select_bg);
		bg.setPosition(Global.sWYSize.width/2, Global.sWYSize.height/2);
		bg.setScale(Global.sScaleRate);
		bg.autoRelease();
		addChild(bg);
		
		initLockSign();
		
		mSelect = Sprite.make(R.drawable.level_select);
		mSelect.setScale(Global.sScaleRate);
		mSelect.autoRelease();
		addChild(mSelect);
		
		Button btnBack = Button.make(R.drawable.btn_back_n, R.drawable.btn_back_p, this, "levelBack");
		btnBack.autoRelease();
		btnBack.setScale(Global.sScaleRate);
		btnBack.setAnchor(0, 0);
		btnBack.setPosition(Global.sStartX, Global.sEndY);
		addChild(btnBack);
		
		mscore = Label.make("", 35);
		mscore.autoRelease();
		mscore.setScale(Global.sScaleRate);
		mscore.setAnchor(1f, 0.5f);
		mscore.setPosition(Global.sEndX-40*Global.sScaleRate, Global.sStartY-422*Global.sScaleRate);//760,422
		addChild(mscore);
		
		Button btnTool = Button.make(R.drawable.btn_tool_n, R.drawable.btn_tool_p, this, "xlTool");
		btnTool.autoRelease();
		btnTool.setAnchor(0.5f, 0f);
		btnTool.setPosition(Global.sWYSize.width/2-16*Global.sScaleRate, Global.sEndY);
		btnTool.setScale(Global.sScaleRate);
		addChild(btnTool);
		
	}
	
	public int getCurLevel() {
		return mCurLvl;
	}

	/**
	 * 进入关卡选择界面的时候，需要设置选择的ui等。
	 */
	public void enterSelectLevel() {
		checkUnlocked();
		
		float posx = mLocked[mCurLvl].getPositionX();
		float posy = mLocked[mCurLvl].getPositionY()+ (mLocked[mCurLvl].getHeight()/2);
		float height = 10*Global.sScaleRate;
		final JumpTo jt = JumpTo.make(1.0f, posx, posy, posx, posy, height, 1);
		jt.autoRelease();
		RepeatForever rf = RepeatForever.make(jt);
		rf.autoRelease();
		mSelect.runAction(rf);
		mSelect.setVisible(true);
		
		mscore.setText(Integer.toString(Util.getGold(GateActivity.sInstance)));
	}
	

	private void checkUnlocked() {
		for(int i=0;i<sPos.length;i++) {
//			if (i == 0) {
//				mLocked[0].setVisible(false);
//			} else {
			mLocked[i].setVisible(checkLevelUnlock(i));
//			}
		}
	}
	
	public void tick() {
		// TODO Auto-generated method stub
		
	}
	/////////////////////////////////////////////////////
//	int[][] mLevelUnlockMS;
	int[] mLevelUnlockM;

	// 解锁关卡
//	public void unlockLevel(int levelMaster, int levelSub) {
//		mLevelUnlockMS[levelMaster][levelSub] = LEVEL_UNLOCK;
//	}

	//
//	public boolean checkLevelUnlock(int levelMaster, int levelSub) {
//		return mLevelUnlockMS[levelMaster][levelSub] == LEVEL_UNLOCK;// || true;
//	}

	// 解锁关卡
	public void unlockLevel(int level) {
		mLevelUnlockM[level] = LEVEL_UNLOCK;
	}

	//
	public boolean checkLevelUnlock(int level) {
		return mLevelUnlockM[level] == LEVEL_UNLOCK;// || true;
	}

	public void loadLockedInfo() {
		// 一维数组关卡加载。
		mLevelUnlockM = com.wb.fw.Util.getUnlocked(GateActivity.sInstance,
				sPos.length);
		// 二维数组关卡加载。
		// mLevelUnlockMS
	}

	public void unlockLevel() {
//		if (mLevelUnlockMS != null) {
			// 二维数组关卡解锁。。有子关口的情况
			// int curSubLevel = mLevelSubSelect.getSubSelected();
			// int curMasterLevel = mLevelSelect.getLevelSelect();
			// if (curSubLevel == LEVEL_SUB_BOSS) {
			// if (curMasterLevel == LEVEL_MASTER_4) {
			//
			// } else {
			// curMasterLevel ++;
			// }
			// } else {
			// curSubLevel ++;
			// }
			// unlockLevel(curMasterLevel, curSubLevel);
			// Util.saveUnlocked(LQZDYActivity.sInstance, mLevelUnlockData);
//		} else {
			// 一维数组关卡解锁。
			// if (mLevelSelect != null) {
			if (mCurLvl < sPos.length - 1) {
				mCurLvl++;
			} else {
				// 已经通关。
				Util.unlockGougou(GateActivity.sInstance);
				GateActivity.sInstance.runOnUiThread(new Runnable() {
					@Override
					public void run() {
					Toast.makeText(GateActivity.sInstance, "可以使用小狗人物！",
							Toast.LENGTH_SHORT).show();
					}
				});
			}
			unlockLevel(mCurLvl);
			com.wb.fw.Util.saveUnlocked(GateActivity.sInstance, mLevelUnlockM);
//		}
	}
	void unlockAll() {
		for (int i=0;i<sPos.length;i++) {
			unlockLevel(i);
		}
	}
////////////////上面是模板/////////////////////////////
	Button[] mLocked;

	void initLockSign() {
		mLocked = new Button[sPos.length];
		for(int i=0;i<sPos.length;i++) {
			mLocked[i] = Button.make(R.drawable.level_unlocked_n,
					R.drawable.level_unlocked_p, R.drawable.level_unlocked_n,
					R.drawable.level_unlocked_n, new TargetSelector(this,
							"xlstartGame(int)", new Object[] { i }));
			mLocked[i].autoRelease();
			mLocked[i].setPosition(Global.sStartX + Util.getScaleX(sPos[i][0]),
					Global.sStartY - Util.getScaleY(sPos[i][1]));
			mLocked[i].setVisible(false);
			mLocked[i].setScale(Global.sScaleRate);
			addChild(mLocked[i]);
		}
	}

	public void xlTool() {
		SoundManager.clickMenuItem();
		mparent.switchState(GS_GAME_SHOP);
	}
	
	public void xlstartGame(int level) {
		SoundManager.clickMenuItem();
		mCurLvl = level;
		mSelect.stopAllActions();
		mparent.switchState(GS_GAME_LEVEL_CONFIRM);
	}

	@Override
	protected boolean onBackButton() {
		if (isVisible()) {
			mSelect.stopAllActions();
			mparent.switchState(GS_GAME_MAIN_MENU);
		}
		return true;
	}

//	@Override
//	public boolean wyTouchesBegan(MotionEvent event) {
//		unlockAll();
//		checkUnlocked();
//		return super.wyTouchesBegan(event);
//	}
//	
	
	/////////////////////////////////////////////
	public void levelBack() {
		SoundManager.clickMenuItem();
		mSelect.stopAllActions();
		mparent.switchState(GS_GAME_MAIN_MENU);
	}
}
